/**
 *  @description 微信白底
 * */
export const weChatInputBug = () => {
  let myFunction
  document.body.addEventListener('focusin', () => {
    clearTimeout(myFunction)
  })
  document.body.addEventListener('focusout', () => {
    clearTimeout(myFunction)
    myFunction = setTimeout(function () {
      window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
    }, 200)
  })
}

/**
 * @description 日期格式转换
 * ios 不支持2019-01-01 日期格式  replace(/-/g, '/')
 * */
export const dateReplace = (end) => {
  let time = new Date().getTime()
  let endTime = new Date(end.replace(/-/g, '/')).getTime()

  return endTime - time
}

/**
 * @description 手机号码正则
 * */
export const phoneRegular = (phone) => {
  return /^1[3-9]\d{9}$/.test(phone)
}

/**
 * @description 手机号分段显示
 * */
export const setPhoneShow = (newValue, oldValue) => {
  if (newValue > oldValue) {
    if (newValue.length === 4 || newValue.length === 9) {
      let pre = newValue.substring(0, newValue.length - 1)
      let last = newValue.substr(newValue.length - 1, 1)
      return pre + ' ' + last
    } else {
      return newValue
    }
  } else {
    if (newValue.length === 9 || newValue.length === 4) {
      return newValue.trim()
    } else {
      return newValue
    }
  }
}

/**
 * @description 去掉空格
 * */
export const replaceReg = (val) => {
  let reg = new RegExp('\\s', 'g')
  return val.replace(reg, '')
}

/**
 * @description 小于9加零函数
 * */
export const addZero = (index) => {
  return index < 9 ? `0${index + 1}` : index + 1
}

/**
 * @description rem转成对应px
 * */
export function remSize (rem) {
  let fontSize = document.documentElement.style.fontSize
  return Math.floor(rem * fontSize.replace('px', ''))
}

/**
 * @description 获得当前主域名
 */
export const getMasterDomain = () => {
  let arr = window.location.hostname.split('.')
  if (arr.length > 2) {
    arr = arr
      .reverse()
      .slice(0, 2)
      .reverse()
  }
  return `.${arr.join('.')}`
}

/**
 * @description 获得当前地址是否是域名地址
 */
export const isDomainAddress = () => {
  let hostname = window.location.hostname
  return !/^\d{1,3}\.\d{1,3}.\d{1,3}.\d{1,3}$/.test(hostname) && hostname.toLowerCase() !== 'localhost'
}

/**
 *  @description 获取url中"?"符后的字串
 * */
export const getRequest = () => {
  let url = decodeURI(window.location.search) // 获取url中"?"符后的字串
  let theRequest = {}
  if (url.indexOf('?') !== -1) {
    let str = url.substring(url.indexOf('?') + 1)
    let strs = str.split('&')
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
    }
  }
  return theRequest
}

/**
 * @description 识别url替换
 * */
export const replaceUrl = (val) => {
  const urlRule = /(?:http|ftp|https):\/\/[\w\-_]+(?:\.[\w\-_]+)+(?:[\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/ig
  if (urlRule.test(val)) {
    val = val.replace(urlRule, (c) => {
      return `<a href="${c}">${c}</a>`
    })
  }
  return val
}

/**
 *  @description 显示弹窗取消页面滚动
 * */
export const popNotScroll = (bool) => {
  const el = document.body
  const noScroll = 'no-scroll'
  if (bool) {
    !el.classList.contains(noScroll) && el.classList.add(noScroll)
  } else {
    el.classList.remove(noScroll)
  }
}
